var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();

Page({
    data: {
      userInfo: {},
      hasMobile: '',
      gradeValue:"暂无等级",
      accountPrice:0,
      brokerage:0,
      fansAllCount:0,
      fansTodayCount:0,
      fansYesterdayCount:0,
      portablePrice:0,
      referrerTime:'',
      referrerAvatar:'',
      referrerName:'暂无推荐人',
    },
    onLoad: function (options){
    },
    onReady: function () {
      this.dialog = this.selectComponent("#dialog");
    },
    onShareAppMessage: function () {
      let shareId = wx.getStorageSync('userId');
      wx.showModal({
        title: '提示',
        content: shareId,
        showCancel: false
      });
      return {
        title: '欢迎注册使用惠美猫！',
        path: '/pages/ucenter/index/index?share_id=' + shareId,
        imageUrl: '/static/images/logo.png',
        success: function (res) { }
      }
    },
    onShow: function () {
        let userInfo = wx.getStorageSync('userInfo');
        let token = wx.getStorageSync('token');
        let that = this;
        let data = new Object();

        // 页面显示
        if (userInfo && token) {
            app.globalData.userInfo = userInfo;
            app.globalData.token = token;
        }

        that.setData({
          userInfo: app.globalData.userInfo
        });

      util.request(api.GetUserInfo).then(function (res) {
        if (res.errno === 0) {
          that.setData({
            accountPrice: res.data.total_account_format,
            brokerage: res.data.commission.toFixed(2),
            portablePrice: res.data.account_format,
            gradeValue: res.data.user_level_name,
            referrerName: res.data.referrer_name,
            fansAllCount: res.data.fans_all_count,
            fansTodayCount: res.data.fans_today_count,
            fansYesterdayCount: res.data.fans_yesterday_count,
            referrerTime: res.data.register_time,
            referrerAvatar: res.data.referrer_avatar
          });
        }
      })
    },
    showDialog() {
      this.dialog.showDialog();
    },
    _cancelEvent() {
      console.log('你点击了取消');
      this.dialog.hideDialog();
    },
    _confirmEvent() {
      console.log('你点击了确定');
      this.dialog.hideDialog();
    },
    bindGetUserInfo(e) {
      let userInfo = wx.getStorageSync('userInfo');
      let token = wx.getStorageSync('token');
      
      if (userInfo && token) {
        return;
      }
        if (e.detail.userInfo){
            //用户按了允许授权按钮
            user.loginByWeixin(e.detail).then(res => {
                this.setData({
                    userInfo: res.data.userInfo
                });
              app.globalData.userInfo = userInfo;
              app.globalData.token = token;

            }).catch((err) => {
                console.log(err)
            });
        } else {
            //用户按了拒绝按钮
            wx.showModal({
                title: '警告通知',
                content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
                success: function (res) {
                    if (res.confirm) {
                        wx.openSetting({
                            success: (res) => {
                                if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
                                    user.loginByWeixin(e.detail).then(res => {
                                        this.setData({
                                          userInfo: res.data.userInfo
                                        });
                                        app.globalData.userInfo = res.data.userInfo;
                                        app.globalData.token = res.data.token;

                                    }).catch((err) => {
                                        console.log(err)
                                    });
                                }
                            }
                        })
                    }
                }
            });
        }
    },
    exitLogin: function () {
        wx.showModal({
            title: '',
            confirmColor: '#b4282d',
            content: '退出登录？',
            success: function (res) {
                if (res.confirm) {
                    wx.removeStorageSync('token');
                    wx.removeStorageSync('userInfo');
                    wx.switchTab({
                        url: '/pages/index/index'
                    });
                }
            }
        })

    }
})